package nc.bs.caa.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

import nc.bs.logging.Logger;


/**
 * Utility class to read the caa&cda configuration.
 *
 * @author ganhaitian
 * @version 
 */

public class CaaNCdaProperties {



    private static Properties properties = null;


    static {

        loadProperties();

    }


    // --------------------------------------------------------- Public Methods


    /**
     * Return specified property value.
     */
    public static String getProperty(String name) {
    
        return properties.getProperty(name);

    }


    /**
     * Return specified property value.
     */
    public static String getProperty(String name, String defaultValue) {

        return properties.getProperty(name, defaultValue);

    }


    // --------------------------------------------------------- Public Methods


    /**
     * Load properties.
     */
    private static void loadProperties() {

        InputStream is = null;
        Throwable error = null;

        try {
            String configUrl = getConfigUrl();
            if (configUrl != null) {
                is = (new URL(configUrl)).openStream();
            }
        } catch (Throwable t) {
            
        }

//        if (is == null) {
//            try {
//                File home = new File(getCatalinaBase());
//                File conf = new File(home, "conf");
//                File properties = new File(conf, "catalina.properties");
//                is = new FileInputStream(properties);
//            } catch (Throwable t) {
//             
//            }
//        }

        if (is == null) {
            try {
                is = CaaNCdaProperties.class.getResourceAsStream
                    ("/nc/bs/caa/util/caancda.properties");
            } catch (Throwable t) {
                //ExceptionUtils.handleThrowable(t);
            }
        }

        if (is != null) {
            try {
                properties = new Properties();
                properties.load(is);
                is.close();
            } catch (Throwable t) {
                error = t;
            }
        }

        if ((is == null) || (error != null)) {
            // Do something
            Logger.warn("Failed to load caancda.properties", error);
            // That's fine - we have reasonable defaults.
            properties=new Properties();
        }

        // Register the properties as system properties
        Enumeration<?> enumeration = properties.propertyNames();
        while (enumeration.hasMoreElements()) {
            String name = (String) enumeration.nextElement();
            String value = properties.getProperty(name);
            if (value != null) {
                System.setProperty(name, value);
            }
        }

    }




    /**
     * Get the value of the configuration URL.
     */
    private static String getConfigUrl() {
        return System.getProperty("catalina.config");
    }


}
